package com.google.code.pathlet.eshop.party.web;

import java.util.Map;

import com.google.code.pathlet.config.anno.InstanceIn;
import com.google.code.pathlet.eshop.party.service.UserService;
import com.google.code.pathlet.eshop.party.vo.UserVo;
import com.google.code.pathlet.eshop.party.vo.UserQueryVo;
import com.google.code.pathlet.vo.QueryResultVo;
import com.google.code.pathlet.web.widget.JqgridQueryConverter;
import com.google.code.pathlet.web.widget.JqgridQueryResult;
import com.google.code.pathlet.web.widget.ResponseLevel;
import com.google.code.pathlet.web.widget.ResponseMessage;

public class UserAction {
 
	@InstanceIn(path="UserService.pb")
	private UserService userService;

	private UserVo user;
	
	private Map<String, String[]> queryParam;

	private String[] delUsernames;
	
	public void setUser(UserVo user) {
		this.user = user;
	}

	public UserVo getUser() {
		return user;
	}

	public String[] getDelUsernames() {
		return delUsernames;
	}

	public void setDelUsernames(String[] delUsernames) {
		
		this.delUsernames = delUsernames;
	}

	public Map<String, String[]> getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(Map<String, String[]> queryParam) {
		this.queryParam = queryParam;
	}

	public JqgridQueryResult<UserVo> query() throws Exception {
		JqgridQueryConverter converter = new JqgridQueryConverter();
		QueryResultVo<UserVo> result = userService.queryUserList(converter.convertParam(queryParam, UserQueryVo.class));
		JqgridQueryResult<UserVo> jqgridResult = converter.convertResult(result, null);
		return jqgridResult;
	}
	
	public String getVersion() {
		return userService.getVersion();
	}

	public ResponseMessage delete() throws Exception {
		userService.deleteUsers(delUsernames);
		return new ResponseMessage(ResponseLevel.INFO, "Delete Success!");
	}

	public UserVo get() throws Exception {
		return userService.getUser(this.user.getUsername());
	}

	public ResponseMessage save() throws Exception {
		userService.saveUser(this.user);
		return new ResponseMessage(ResponseLevel.INFO, "Save Success!");
	}
	
	public ResponseMessage update() throws Exception {
		userService.updateUser(this.user);
		return new ResponseMessage(ResponseLevel.INFO, "Update Success!");
	}

}